//
//  AddDangQiViewController.m
//  zhuchiren
//
//  Created by rainday on 15/2/8.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "AddDangQiViewController.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import <UIDatePicker+RACSignalSupport.h>
#import "DangQiViewModel.h"

@interface AddDangQiViewController ()<UITextFieldDelegate>

@end

@implementation AddDangQiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _saveButton.layer.cornerRadius = 5;
    _saveButton.layer.masksToBounds = YES;
    
    _beginTime.text = _beinTimeText;
    _endTime.text = _endTimeText;
    _titleLabel.text = _titleText;
    [_saveButton setTitle:_saveButtonTitle forState:UIControlStateNormal];
    _noteTextFeild.text = _noteText;
    
    @weakify(self);
    RAC(_saveButton,enabled) = [RACSignal combineLatest:@[_noteTextFeild.rac_textSignal,_beginTime.rac_textSignal,_endTime.rac_textSignal] reduce:^id(NSString *note,NSString *beginTime,NSString *endTime){
        return @(([note length]>0 && [beginTime length]>0 && [endTime length]>0));
    }];
    
    RAC(_saveButton,backgroundColor) = [RACSignal combineLatest:@[_noteTextFeild.rac_textSignal,_beginTime.rac_textSignal,_endTime.rac_textSignal] reduce:^id(NSString *note,NSString *beginTime,NSString *endTime){
        return ([note length]>0 && [beginTime length]>0 && [endTime length]>0)?[UIColor redColor]:[UIColor lightGrayColor];
        
        
    }];
    
    
    [[_saveButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        [_delegate addDangQiViewController:self updateSchedule:_pk beginTime:_beginTime.text endTime:_endTime.text note:_noteTextFeild.text];
        
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)disMissSelf:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)textFieldDidBeginEditing:(UITextField *)textField{
 
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-CN"];
    datePicker.locale = locale;
    datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    NSTimeZone *zone =[NSTimeZone systemTimeZone];
    datePicker.timeZone = zone;
    textField.inputView = datePicker;
    
    
    @weakify(textField);
    [[datePicker rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(UIDatePicker *datePicker) {
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        
        
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        
        @strongify(textField);
        textField.text = [dateFormatter stringFromDate:datePicker.date];
        
        
    }];
    
    // 建立 UIToolbar
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIBarButtonItem *right = [[UIBarButtonItem alloc]init];
    [right setStyle:UIBarButtonItemStyleDone];
    right.title = NSLocalizedString(@"完成", nil);
    
    right.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        [textField resignFirstResponder];
        // An empty signal is a signal that completes immediately.
        return [RACSignal empty];
    }];
    
    
    toolBar.items = [NSArray arrayWithObjects:right,nil];
    textField.inputAccessoryView = toolBar;

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
